<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>绘制矩形</title>
  <style>
    #canvas {
      border: 1px solid #CCC;
    }
  </style>
</head>

<body>
  <canvas id="canvas" width="500" height="300"></canvas>
  <script>
    /** @type {HTMLCanvasElement} */
    const canvas = document.getElementById('canvas');
    /** @type {CanvasRenderingContext2D} */
    const ctx = canvas.getContext('2d');
    ctx.strokeRect(50, 50, 100, 100);
    ctx.fillRect(100, 100, 100, 100);

    let height = 0;
    const timer = setInterval(() => {
      height++;
      ctx.clearRect(0, 0, canvas.clientWidth, height);
      if (height > canvas.clientHeight) clearInterval(timer);
    }, 10);
  </script>
</body>

</html>